package com.lagou.servlet;

import com.lagou.utils.JdbcUtil;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author 13771
 * @date 2020/9/8 23:14
 * @description
 */
@WebServlet(name="demo",urlPatterns = "/demo")
public class MyServlet extends HttpServlet {
    private  static ZkClient zkClient = null;
    private static String path = "";
    private JdbcUtil jdbcUtil = null;
    private String url;
    private String user;
    private String password;
    private String driver;
    static {
        zkClient = new ZkClient("127.0.0.1:2181");
        path = "/configCenter";
        String config = "url=jdbc:mysql://localhost:3306/test&username=root&password=123456&driverClassName=com.mysql.jdbc.Driver";
        zkClient.createEphemeral(path,config);
        System.out.println("配置信息成功注册到zk。。。");
    }


    @Override
    public void init() throws ServletException {

        String getConfig = zkClient.readData(path);
        String[] split = getConfig.split("&");
        url = split[0].split("=")[1]+"?characterEncoding=utf8&useSSL=false";
        user = split[1].split("=")[1];
        password = split[2].split("=")[1];
        driver = split[3].split("=")[1];

        System.out.println("读取初始化配置完成。。。");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            jdbcUtil = new JdbcUtil(url,user,password,driver);
            String sql = "select * from tb_resume";
            List<Map<String, Object>> maps = jdbcUtil.executeQuery(sql);
            System.out.println(maps);

        try {
            //注册监听，监听配置变更事件
            zkClient.subscribeDataChanges(path, new IZkDataListener() {
                @Override
                public void handleDataChange(String s, Object o) throws Exception {
                    System.out.println("配置信息已变更。。。");
                    init();
                    jdbcUtil = new JdbcUtil(url,user,password,driver);
                }

                @Override
                public void handleDataDeleted(String s) throws Exception {
                    System.out.println("配置信息已删除。。。");
                }
            });

//            String updateConfig = "url=jdbc:mysql://127.0.0.1:3306/test&username=root&password=123456&driverClassName=com.mysql.jdbc.Driver";
//            zkClient.writeData(path,updateConfig);
            Thread.sleep(5000);
            List<Map<String, Object>> map2 = jdbcUtil.executeQuery(sql);
            System.out.println(map2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

}
